package com.example.shiyansan;

import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class MainActivity extends AppCompatActivity {
    private TextView resultTextView;
    private static final int UPDATE_PROGRESS = 0x111;
    private ProgressBar progressBar;
    private AlertDialog progressDialog;
    private int progressStatus = 0;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == UPDATE_PROGRESS) {
                progressBar.setProgress(progressStatus);
                if (progressStatus >= 100) {
                    progressDialog.dismiss();
                    Toast.makeText(MainActivity.this, "任务完成", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化UI组件
        resultTextView = findViewById(R.id.resultTextView);
        Button activityListenerBtn = findViewById(R.id.activityListenerBtn);
        Button innerClassBtn = findViewById(R.id.innerClassBtn);
        Button anonymousBtn = findViewById(R.id.anonymousBtn);
        Button lambdaBtn = findViewById(R.id.lambdaBtn);
        Button outerClassBtn = findViewById(R.id.outerClassBtn);
        Button tagBtn = findViewById(R.id.tagBtn);
        Button systemInfoBtn = findViewById(R.id.systemInfoBtn);
        Button progressBtn = findViewById(R.id.progressBtn);

        // 设置监听器（使用匿名内部类替代接口实现，避免强制要求onClick方法）
        activityListenerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultTextView.setText("Activity作为监听器");
            }
        });

        innerClassBtn.setOnClickListener(new InnerClassListener());

        tagBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultTextView.setText("标签绑定监听器");
            }
        });

        // 匿名内部类监听器
        anonymousBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultTextView.setText("匿名内部类监听器");
            }
        });

        // Lambda表达式监听器
        lambdaBtn.setOnClickListener(v -> resultTextView.setText("Lambda表达式监听器"));

        // 外部类监听器
        outerClassBtn.setOnClickListener(new OuterClassListener(resultTextView));

        // 系统信息按钮
        systemInfoBtn.setOnClickListener(v -> showSystemInfo());

        // 进度条按钮
        progressBtn.setOnClickListener(v -> showProgressDialog());
    }

    // 内部类监听器
    private class InnerClassListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            resultTextView.setText("内部类监听器");
        }
    }

    // 外部类监听器
    static class OuterClassListener implements View.OnClickListener {
        private final TextView textView;

        public OuterClassListener(TextView textView) {
            this.textView = textView;
        }

        @Override
        public void onClick(View v) {
            textView.setText("外部类监听器");
        }
    }

    // 显示系统信息
    private void showSystemInfo() {
        Configuration config = getResources().getConfiguration();
        StringBuilder info = new StringBuilder();

        info.append("屏幕方向: ")
                .append(config.orientation == Configuration.ORIENTATION_LANDSCAPE ? "横向" : "纵向")
                .append("\nMNC代码: ")
                .append(config.mnc)
                .append("\n导航方式: ");

        switch (config.navigation) {
            case Configuration.NAVIGATION_NONAV:
                info.append("无导航控制");
                break;
            case Configuration.NAVIGATION_WHEEL:
                info.append("滚轮导航");
                break;
            case Configuration.NAVIGATION_DPAD:
                info.append("方向键导航");
                break;
            case Configuration.NAVIGATION_TRACKBALL:
                info.append("轨迹球导航");
                break;
            default:
                info.append("未知");
        }

        info.append("\n触摸屏类型: ");
        switch (config.touchscreen) {
            case Configuration.TOUCHSCREEN_NOTOUCH:
                info.append("无触摸屏");
                break;
            case Configuration.TOUCHSCREEN_STYLUS:
                info.append("触摸笔式");
                break;
            case Configuration.TOUCHSCREEN_FINGER:
                info.append("手指触摸式");
                break;
            default:
                info.append("未知");
        }

        Toast.makeText(this, info.toString(), Toast.LENGTH_LONG).show();
    }

    // 显示进度对话框
    private void showProgressDialog() {
        View dialogView = getLayoutInflater().inflate(R.layout.progress_dialog, null);
        progressBar = dialogView.findViewById(R.id.progressBar);

        progressDialog = new MaterialAlertDialogBuilder(this)
                .setTitle("任务进度")
                .setView(dialogView)
                .setCancelable(false)
                .create();

        progressDialog.show();

        // 重置进度
        progressStatus = 0;
        progressBar.setProgress(0);

        // 模拟进度更新
        new Thread(() -> {
            while (progressStatus < 100) {
                progressStatus += 1;
                handler.sendEmptyMessage(UPDATE_PROGRESS);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}